package com.xuanheng.healthscale;

import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.xuanheng.pojo.ScaleData;
import com.xuanheng.utils.BleScaleListener;
import com.xuanheng.utils.BleScaleUtil;
import com.xuanheng.utils.StringUtil;


public class TestActivity extends AppCompatActivity {
    private EditText et1, et2, et3;
    private EditText editText;
    private TextView txt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        et1 = (EditText) findViewById(R.id.et1);
        et2 = (EditText) findViewById(R.id.et2);
        et3 = (EditText) findViewById(R.id.et3);
        findViewById(R.id.btn1).setOnClickListener(onClickListener);
        txt= (TextView) findViewById(R.id.txt);
        editText= (EditText) findViewById(R.id.edt);
        BleScaleUtil.getInstance().setBleScaleListener(bleScaleListener);
    }




    private View.OnClickListener onClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.btn1:
                    BleScaleUtil.getInstance().startScan(et1.getText().toString(),et2.getText().toString(),et3.getText().toString());
                    break;
            }
        }
    };

    private void setState(String state){
        txt.setText("状态:"+state);
    }
    private BleScaleListener bleScaleListener=new BleScaleListener() {
        @Override
        public void onConnect() {
            setState("连接成功");
        }

        @Override
        public void onConnectFail() {
            setState("连接失败");
        }

        @Override
        public void onConnectting() {
            setState("连接中。。。");
        }

        @Override
        public void onDisconnect() {
            setState("连接断开");
        }

        @Override
        public void onReciveData(final byte[] bytes) {
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {

                    ScaleData scaleData=ScaleData.fromByteArray(bytes);
                    editText.setText(scaleData.toString());
                }
            });

        }

        @Override
        public void onReciveError(String err) {
            setState("获取信息错误"+err);
        }
    };
}
